//
//  ViewController.m
//  UI18_Work 数据库
//
//  Created by dllo on 16/8/8.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#import "AddViewController.h"
#import "SQLiteTool.h"
#import "Person.h"
#import "ContentViewController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate,AddViewControllerDelegate, ContentViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contactArray;
@property (nonatomic, strong) SQLiteTool *sqliteTool;

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
    [self createTableView];
    
}

- (void)createData {
    self.dataArray = [NSMutableArray array];
    _sqliteTool = [SQLiteTool sharedSQLiteTool];
    [_sqliteTool openSQLite3];
    [_sqliteTool createTable];
    self.dataArray = [_sqliteTool selectAll];
}

- (void)add {
    AddViewController *addVC = [[AddViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
    [self.tableView reloadData];
}


- (void)contact:(Person *)contact {
    [self.dataArray addObject:contact];
    if (contact.name.length == 0) {
        [self.dataArray removeLastObject];
    } else {
    [_sqliteTool insertContact:contact];
    // 一定要刷新tableView
    [self.tableView reloadData];
    }
}

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.textLabel.text = [self.dataArray[indexPath.row] name];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ContentViewController *vc = [[ContentViewController alloc] init];
    vc.delegate = self;
    vc.person = self.dataArray[indexPath.row];
    vc.index = indexPath.row;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)sendPerson:(Person *)person index:(NSInteger)index{
    [self.dataArray removeObjectAtIndex:index];
    [self.dataArray insertObject:person atIndex:index];
    [self.sqliteTool deleteAll];
    for (Person *per in self.dataArray) {
        [self.sqliteTool insertContact:per];
    }
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [_sqliteTool deleteAll];
    for (Person *per in self.dataArray) {
        [_sqliteTool insertContact:per];
    }
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
